package com.idroid.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.nfc.NfcAdapter;
import android.nfc.NfcManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ListAdapter;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 各种检查工具类
 */
public class CheckUtil {

    /**
     * 检查银行卡是否合法
     * isLegalBankCardNum
     *
     * @param ctx
     * @param card
     */
    public static boolean isBankCardNum(Context ctx, String card) {
        boolean isCard = false;
        String regex = "^\\d{10,24}$";
        isCard = Pattern.matches(regex, card);
        if (!isCard) {
            MToast.shortToast(ctx, "银行卡号错误");
        }
        return isCard;
    }

    /**
     * 对身份证号进行验证
     * isIDCardNumber
     *
     * @param ctx
     * @param id
     * @return
     */
    public static boolean isIDCardNum(Context ctx, String id) {
        String regex = "([0-9]{17}([0-9]|X))|([0-9]{15})";
        boolean isTrue = false;
        isTrue = Pattern.matches(regex, id);
        if (!isTrue) {
            MToast.shortToast(ctx, "您输入的身份证号码不正确");
        }
        return isTrue;
    }

    /**
     * 检查手机号
     * isLegalPhoneNum
     *
     * @param ctx
     * @param mobile
     * @return
     */
    public static boolean isMobileNum(Context ctx, String mobile) {
        boolean isTel = false;
        isTel = Pattern.matches("^(1)\\d{10}$", mobile);
        if (!isTel) {
            MToast.shortToast(ctx, "您输入的手机号码不正确");
        }
        return isTel;
    }

    /**
     * 检测银行卡密码、支付密码是否是6位数字的密码
     * isLegalSixIntePwd
     *
     * @param ctx
     * @param pwd
     * @return
     */
    public static boolean is6InterPwd(Context ctx, String pwd) {
        boolean isPwd = false;
        String regex = "^[0-9]{6}$";
        isPwd = Pattern.matches(regex, pwd);
        if (!isPwd) {
            MToast.shortToast(ctx, "您输入的密码不正确");
        }
        return isPwd;
    }

    /**
     * 检测4位验证码
     * isLegalCaptcha
     *
     * @param ctx
     * @param pwd
     * @return
     */
    public static boolean isLegalCaptcha(Context ctx, String pwd) {
        boolean isPwd = false;
        String regex = "^[0-9]{4}$";
        isPwd = Pattern.matches(regex, pwd);
        if (!isPwd) {
            MToast.shortToast(ctx, "您输入的验证码不正确");
        }
        return isPwd;
    }

    /**
     * 检测该字符是否是x
     * isStr
     *
     * @param text
     * @return
     */
    public static boolean isStringX(String text) {
        try {
            if (text.toUpperCase().endsWith("X")) {
                return true;
            }
        } catch (Exception e) {
            return false;
        }
        return false;
    }

    /**
     * 检测是否是纯数字
     * isNum
     *
     * @param text
     * @return
     */
    public static boolean isNumber(Context ctx, String text) {
        Pattern p = Pattern.compile("[0-9]*");
        Matcher m = p.matcher(text);
        if (m.matches()) {
            return true;
        }
        MToast.shortToast(ctx, "您输入的不是纯数字");
        return false;
    }

    /**
     * 检测是否为纯字母
     *
     * @param txt
     * @return
     */
    public static boolean isA2Z(Context ctx, String txt) {
        Pattern p = Pattern.compile("[a-zA-Z]");
        Matcher m = p.matcher(txt);
        if (m.matches()) {
            return true;
        }
        MToast.shortToast(ctx, "您输入的不是纯字母");
        return false;
    }

    /**
     * 检测是否为纯汉字
     * 一般用于认证时名字校验使用
     *
     * @param txt
     * @return
     */
    public static boolean isChines(Context ctx, String txt) {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]");
        Matcher m = p.matcher(txt);
        if (m.matches()) {
            return true;
        }
        MToast.shortToast(ctx, "您输入的不是纯汉字");
        return false;
    }

    public static boolean hasNfc(Context context){
        boolean bRet=false;
        if(context==null)
            return bRet;
        NfcManager manager = (NfcManager) context.getSystemService(Context.NFC_SERVICE);
        NfcAdapter adapter = manager.getDefaultAdapter();
        if (adapter != null && adapter.isEnabled()) {
            // adapter存在，能启用
            bRet=true;
        }
        return bRet;
    }

    @SuppressWarnings("unused")
    public static void setListViewHeight(ExpandableListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        int totalHeight = 0;
        int count = listAdapter.getCount();
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }

    public static Bitmap getBitmap(String url) {
        Bitmap bm = null;
        try {
            URL iconUrl = new URL(url);
            URLConnection conn = iconUrl.openConnection();
            HttpURLConnection http = (HttpURLConnection) conn;

            int length = http.getContentLength();

            conn.connect();
            // 获得图像的字符流
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is, length);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();// 关闭流
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        return bm;
    }

    public static Bitmap GetLocalOrNetBitmap(String url)
    {
        Bitmap bitmap = null;
        InputStream in = null;
        BufferedOutputStream out = null;
        try
        {
            in = new BufferedInputStream(new URL(url).openStream(), 2*1024);
            final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
            out = new BufferedOutputStream(dataStream, 2*1024);
            //copy(in, out);
            out.flush();
            byte[] data = dataStream.toByteArray();
            bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
            data = null;
            return bitmap;
        }
        catch (IOException e)
        {
            e.printStackTrace();
            return null;
        }
    }

}
